package org.aplikator.client.shared.rpc.marshaller;

import org.aplikator.client.shared.descriptor.ApplicationDTO;
import org.aplikator.client.shared.descriptor.MenuDTO;
import org.jboss.errai.marshalling.client.api.MarshallingSession;
import org.jboss.errai.marshalling.client.api.annotations.ClientMarshaller;
import org.jboss.errai.marshalling.client.api.annotations.ServerMarshaller;
import org.jboss.errai.marshalling.client.api.json.EJObject;
import org.jboss.errai.marshalling.client.api.json.EJValue;
import org.jboss.errai.marshalling.client.marshallers.AbstractNullableMarshaller;

import java.util.List;
import java.util.Map;
import java.util.Set;


@ClientMarshaller(ApplicationDTO.class)
@ServerMarshaller(ApplicationDTO.class)
public class ApplicationDTOMarshaller extends
		AbstractNullableMarshaller<ApplicationDTO> {

	public ApplicationDTOMarshaller() {
		super();
	}

	/*@Override
	public Class<ApplicationDTO> getTypeHandled() {
		return ApplicationDTO.class;
	}*/

	@Override
	public ApplicationDTO[] getEmptyArray() {
		return new ApplicationDTO[0];
	}

	@Override
	public ApplicationDTO doNotNullDemarshall(EJValue o, MarshallingSession ctx) {
		if (o.isObject() != null)  {
			ApplicationDTO retval = new ApplicationDTO();
			EJObject object = o.isObject();
			if (object.get("brand").isString() != null) {
				retval.setBrand(object.get("brand").isString().stringValue());
			}
			if (object.get("showNavigation").isBoolean()!=null) {
				retval.setShowNavigation(object.get("showNavigation").isBoolean().booleanValue());
			}
			if (object.containsKey("defaultActionToken") && object.get("defaultActionToken").isString()!=null) {
				retval.setDefaultAction(object.get("defaultActionToken").isString().stringValue());
			}
			
			if (object.get("config").isObject() != null) {
				Map  map = DemarshallingUtils.mapUnMarshall(object.get("config"), ctx, String.class.getName());
				Set keySet = map.keySet();
				for (Object ob : keySet) {
					retval.setConfigString(ob.toString(), map.get(ob).toString());
				}
			}

			if (object.get("menus").isArray() != null) {
				List list = DemarshallingUtils.listUnMarshall(object.get("menus"), ctx, MenuDTO.class.getName());
				for (Object ob : list) {
					MenuDTO mdto = (MenuDTO) ob;
					retval.addMenu(mdto);
				}
			}
			return retval;
		} else return null;
	}

	@Override
	public String doNotNullMarshall(ApplicationDTO o, MarshallingSession ctx) {
		StringBuilder builder = new StringBuilder("{");
		MarshallingUtils.string("brand", o.getBrand(), builder).append(',');
		MarshallingUtils.bool("showNavigation", o.isShowNavigation(), builder).append(',');
		
		MarshallingUtils.key("menus", builder).append(':');
		MarshallingUtils.listMarshall(builder, o.getMenus(), ctx, MenuDTO.class.getName()).append(',');
		
		MarshallingUtils.key("config", builder).append(':');
		MarshallingUtils.mapMarshall(builder, o.getConfig(), ctx, String.class.getName());
		
		//defaultActionToken
		if (o.getDefaultActionToken() != null) {
			builder.append(',');
			MarshallingUtils.string("defaultActionToken", o.getDefaultActionToken(), builder);
		}
		
		builder.append("}");
		return builder.toString();
	}
}